<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="1 概念当Consul中注册的服务信息发生变化的时候，我们除了定时通过接口去查询最新的服务信息之外，consul还提供了watch机制，通过监控consul数据的变化，主动通知。 目前conusl watch支持两种通知方式：可执行程序和Http接口。 consul watch支持监控的数据类型：  services - 监控指定列表服务的可用性 service - 监控指定服务的实例 nodes">
<meta property="og:type" content="article">
<meta property="og:title" content="Consul - Watches监控主动通知">
<meta property="og:url" content="http://littlefxc.github.io/2021/01/13/Consul-Watches%E7%9B%91%E6%8E%A7%E4%B8%BB%E5%8A%A8%E9%80%9A%E7%9F%A5/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="1 概念当Consul中注册的服务信息发生变化的时候，我们除了定时通过接口去查询最新的服务信息之外，consul还提供了watch机制，通过监控consul数据的变化，主动通知。 目前conusl watch支持两种通知方式：可执行程序和Http接口。 consul watch支持监控的数据类型：  services - 监控指定列表服务的可用性 service - 监控指定服务的实例 nodes">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-01-13T02:04:09.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:48.940Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="consul">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/2021/01/13/Consul-Watches%E7%9B%91%E6%8E%A7%E4%B8%BB%E5%8A%A8%E9%80%9A%E7%9F%A5/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>Consul - Watches监控主动通知 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E6%A6%82%E5%BF%B5"><span class="nav-text">1 概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95"><span class="nav-text">2 基本用法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-%E9%80%9A%E8%BF%87%E5%8F%AF%E6%89%A7%E8%A1%8C%E7%A8%8B%E5%BA%8F%E9%80%9A%E7%9F%A5"><span class="nav-text">2.1 通过可执行程序通知</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-%E9%80%9A%E8%BF%87Http%E6%8E%A5%E5%8F%A3%E9%80%9A%E7%9F%A5"><span class="nav-text">2.2 通过Http接口通知</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E7%9B%91%E6%8E%A7%E6%9C%8D%E5%8A%A1"><span class="nav-text">3 监控服务</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E7%9B%91%E6%8E%A7%E9%94%AE%E5%80%BC%E6%95%B0%E6%8D%AE"><span class="nav-text">4 监控键值数据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E7%9B%91%E6%8E%A7%E8%8A%82%E7%82%B9"><span class="nav-text">5 监控节点</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%BA%90"><span class="nav-text">参考资源</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/13/Consul-Watches%E7%9B%91%E6%8E%A7%E4%B8%BB%E5%8A%A8%E9%80%9A%E7%9F%A5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Consul - Watches监控主动通知
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-13 10:04:09" itemprop="dateCreated datePublished" datetime="2021-01-13T10:04:09+08:00">2021-01-13</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83/" itemprop="url" rel="index"><span itemprop="name">注册中心</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="1-概念"><a href="#1-概念" class="headerlink" title="1 概念"></a>1 概念</h1><p>当Consul中注册的服务信息发生变化的时候，我们除了定时通过接口去查询最新的服务信息之外，consul还提供了watch机制，通过监控consul数据的变化，主动通知。</p>
<p>目前conusl watch支持两种通知方式：<strong>可执行程序</strong>和<strong>Http接口</strong>。</p>
<p>consul watch支持监控的数据类型：</p>
<ul>
<li>services - 监控指定列表服务的可用性</li>
<li>service - 监控指定服务的实例</li>
<li>nodes - 监控节点</li>
<li>key - 监控特定的键值对</li>
<li>keyprefix - 监控consul配置中心的前缀</li>
<li>checks - 监控健康检查的值</li>
<li>event - 监控自定义用户事件</li>
</ul>
<p>也就是说，consul支持监控服务、键值数据、节点信息，只要发生变化，就通知我们。</p>
<h1 id="2-基本用法"><a href="#2-基本用法" class="headerlink" title="2 基本用法"></a>2 基本用法</h1><p>我们以监控键值数据的变化为例子，介绍watch机制的用法。</p>
<p>watch监控的配置信息是跟consul的配置文件放在一起的，通过配置文件中的watches字段，设置监控信息。</p>
<p>说明：watches的配置，大致用法就是通过type，配置监控数据的类型，然后根据不同数据类型配置不同的参数，最后选择一种通知方式进行配置。</p>
<h2 id="2-1-通过可执行程序通知"><a href="#2-1-通过可执行程序通知" class="headerlink" title="2.1 通过可执行程序通知"></a>2.1 通过可执行程序通知</h2><p>当我们监控的数据发生变化，consul agent会调用我们配置的可执行程序（命令、脚本等等），并且通过标准输入，以Json格式传入通知的参数， 我们只要在程序中根据参数处理业务即可。</p>
<p>例子：</p>
<p>下面是我们consul agent的配置，文件名是 <code>/etc/consul/watches.json</code> 。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="attr">&quot;datacenter&quot;</span>: <span class="string">&quot;dc1&quot;</span>, </span><br><span class="line">	<span class="attr">&quot;data_dir&quot;</span>: <span class="string">&quot;/var/consul&quot;</span>, </span><br><span class="line">	<span class="attr">&quot;ui&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">	<span class="attr">&quot;watches&quot;</span>: [&#123;</span><br><span class="line">		<span class="attr">&quot;type&quot;</span>: <span class="string">&quot;key&quot;</span>,</span><br><span class="line">		<span class="attr">&quot;key&quot;</span>: <span class="string">&quot;foo/bar/baz&quot;</span>,</span><br><span class="line">		<span class="attr">&quot;handler_type&quot;</span>: <span class="string">&quot;script&quot;</span>,</span><br><span class="line">		<span class="attr">&quot;args&quot;</span>: [<span class="string">&quot;/usr/bin/my-service-handler.sh&quot;</span>, <span class="string">&quot;-redis&quot;</span>]</span><br><span class="line">	&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个例子的意思，监控<code>key=foo/bar/baz</code>的键值数据，如果数据发生变化，就调用<code>/usr/bin/my-service-handler.sh -redis</code> 命令。</p>
<p>参数说明：</p>
<ul>
<li><p>datacenter - 数据中心的名字</p>
</li>
<li><p>data_dir - consul数据存放目录</p>
</li>
<li><p>ui - 开启consul ui</p>
</li>
<li><p>watches - 配置监控信息，如果监控的数据发生变化，则根据配置执行通知。</p>
<p>watches参数说明：</p>
<ul>
<li>type - 监控的数据类型</li>
<li>key - 监控的键值数据的Key</li>
<li>handler_type - 通知类型，支持script和http</li>
<li>args - 配置通知类型为script的，执行命令，是一个数组，第一个元素是命令，后面第2个到第N个元素是命令的参数。</li>
</ul>
</li>
</ul>
<p>例子的<code>watches</code>配置的意思就是：</p>
<p>监控<code>key=foo/bar/baz</code>的键值数据，如果数据发生变化，就调用<code>/usr/bin/my-service-handler.sh -redis</code> 命令， 这个命令可以通过标准输入，接收变化的数据。</p>
<p>启动consul，就会加载watches配置。</p>
<h2 id="2-2-通过Http接口通知"><a href="#2-2-通过Http接口通知" class="headerlink" title="2.2 通过Http接口通知"></a>2.2 通过Http接口通知</h2><p>通过http接口通知数据变化，大体上配置跟上面一样，区别是多了一些http接口的配置参数。</p>
<p>例子：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	<span class="attr">&quot;datacenter&quot;</span>: <span class="string">&quot;dc1&quot;</span>,</span><br><span class="line">	<span class="attr">&quot;data_dir&quot;</span>: <span class="string">&quot;/Users/jogin/Documents/work/local/consul/data&quot;</span>,</span><br><span class="line">	<span class="attr">&quot;ui&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">	<span class="attr">&quot;watches&quot;</span>: [&#123;</span><br><span class="line">		<span class="attr">&quot;type&quot;</span>: <span class="string">&quot;key&quot;</span>,</span><br><span class="line">		<span class="attr">&quot;key&quot;</span>: <span class="string">&quot;foo/bar/baz&quot;</span>,</span><br><span class="line">		<span class="attr">&quot;handler_type&quot;</span>: <span class="string">&quot;http&quot;</span>,</span><br><span class="line">		<span class="attr">&quot;http_handler_config&quot;</span>: &#123;</span><br><span class="line">			<span class="attr">&quot;path&quot;</span>: <span class="string">&quot;&lt;https://localhost:8000/watch&gt;&quot;</span>,</span><br><span class="line">			<span class="attr">&quot;method&quot;</span>: <span class="string">&quot;POST&quot;</span>,</span><br><span class="line">			<span class="attr">&quot;header&quot;</span>: &#123;</span><br><span class="line">				<span class="attr">&quot;x-foo&quot;</span>: [<span class="string">&quot;bar&quot;</span>, <span class="string">&quot;baz&quot;</span>]</span><br><span class="line">			&#125;,</span><br><span class="line">			<span class="attr">&quot;timeout&quot;</span>: <span class="string">&quot;10s&quot;</span>,</span><br><span class="line">			<span class="attr">&quot;tls_skip_verify&quot;</span>: <span class="literal">false</span></span><br><span class="line">		&#125;</span><br><span class="line">	&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个例子的意思，就是当<code>key=foo/bar/baz</code>的键值数据发生变化，就通过<code>https://localhost:8000/watch</code>通知我们。</p>
<p>我们关注<code>watches</code>字段的配置信息，下面是参数说明：</p>
<ul>
<li><p>type - watch监控类型是key</p>
</li>
<li><p>key - 监控foo/bar/baz这个key</p>
</li>
<li><p>handler_type - 通知类型, http</p>
</li>
<li><p>http_handler_config - 配置http通知信息。</p>
<p>http_handler_config参数说明：</p>
<ul>
<li>path - 通知Url</li>
<li>method - http请求方法</li>
<li>header - 自定义Http请求头，没有可以忽略</li>
<li>timeout - 超时时间，10秒</li>
<li>tls_skip_verify - 是否跳过tls验证</li>
</ul>
</li>
</ul>
<h1 id="3-监控服务"><a href="#3-监控服务" class="headerlink" title="3 监控服务"></a>3 监控服务</h1><p>上面介绍了watch的基本用法，我们也可以监控服务信息的变化，例如当有人注册新的服务或者服务不可用的时候，通知我们。</p>
<p>我们忽略掉，consul agent的配置，单独看watches的配置。</p>
<p>监控所有的服务的配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	&quot;watches&quot;: [&#123;</span><br><span class="line">		&quot;type&quot;: &quot;services&quot;,</span><br><span class="line">		&quot;handler_type&quot;: &quot;http&quot;,</span><br><span class="line">		&quot;http_handler_config&quot;: &#123;</span><br><span class="line">			...忽略...</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>监控单个服务的配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	&quot;watches&quot;: [&#123;</span><br><span class="line">		&quot;type&quot;: &quot;service&quot;,</span><br><span class="line">                &quot;service&quot;: &quot;要监控的服务名&quot;,</span><br><span class="line">		&quot;handler_type&quot;: &quot;http&quot;,</span><br><span class="line">		&quot;http_handler_config&quot;: &#123;</span><br><span class="line">			...忽略...</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="4-监控键值数据"><a href="#4-监控键值数据" class="headerlink" title="4 监控键值数据"></a>4 监控键值数据</h1><p>前面介绍过基本Key的监控，其实我们还可以通过key的前缀，批量监控一批key，只要key的前缀相同，这些Key下面的数据发生变化，都会发送通知。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	&quot;watches&quot;: [&#123;</span><br><span class="line">		&quot;type&quot;: &quot;keyprefix&quot;,</span><br><span class="line">		&quot;prefix&quot;: &quot;foo&#x2F;&quot;,</span><br><span class="line">		&quot;args&quot;: [&quot;&#x2F;usr&#x2F;bin&#x2F;my-service-handler.sh&quot;, &quot;-redis&quot;]</span><br><span class="line">	&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>监控类型为：keyprefix</p>
<p>通过prefix字段，配置key的前缀。</p>
<p>这个配置的意思就是：以foo/开头的Key, 数据发生变化，都会执行args参数，配置的命令。</p>
<h1 id="5-监控节点"><a href="#5-监控节点" class="headerlink" title="5 监控节点"></a>5 监控节点</h1><p>我们也可以监控consul集群节点的变化信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">	&quot;watches&quot;: [&#123;</span><br><span class="line">		&quot;type&quot;: &quot;nodes&quot;,</span><br><span class="line">		&quot;handler_type&quot;: &quot;http&quot;,</span><br><span class="line">		&quot;http_handler_config&quot;: &#123;</span><br><span class="line">			...忽略...</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>没有其他额外的参数，type=nodes即可，当节点信息发生变化，会根据配置的方式通知。</p>
<h1 id="参考资源"><a href="#参考资源" class="headerlink" title="参考资源"></a>参考资源</h1><p><a target="_blank" rel="noopener" href="https://www.consul.io/docs/dynamic-app-config/watches">Consul by HashiCorp</a></p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/ExMan/p/11907491.html">ExplorerMan</a></p>
<p><a target="_blank" rel="noopener" href="https://www.tizi365.com/archives/535.html">Consul Watches监控服务变化</a></p>
<p><a target="_blank" rel="noopener" href="https://toutiao.io/posts/zeu435/preview">开发者头条</a></p>
<p><a target="_blank" rel="noopener" href="https://www.notion.so/Consul-6982c01eb1594549ab0a6dd463f16cfd">Consul 单机集群搭建</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/consul/" rel="tag"># consul</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/01/12/ES-%E6%89%B9%E9%87%8F%E6%93%8D%E4%BD%9C-bulk/" rel="prev" title="ES 批量操作 bulk">
                  <i class="fa fa-chevron-left"></i> ES 批量操作 bulk
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/01/13/ES%E9%9B%86%E7%BE%A4%E9%85%8D%E7%BD%AE/" rel="next" title="ES集群配置">
                  ES集群配置 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
